package com.jcw.kqxt.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.jwt.JWTUtil;
import cn.hutool.jwt.signers.JWTSigner;
import cn.hutool.jwt.signers.JWTSignerUtil;
import com.jcw.kqxt.constants.Constant;
import com.jcw.kqxt.domain.User;
import com.jcw.kqxt.dto.LoginDto;
import com.jcw.kqxt.mapper.LoginMapper;
import com.jcw.kqxt.mapper.RoleMapper;
import com.jcw.kqxt.result.JSONResult;
import com.jcw.kqxt.service.IloginService;
import com.jcw.kqxt.vo.TokenVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoginServiceImpl implements IloginService {

    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    /**
     * 登录校验
     */
    public JSONResult login(LoginDto dto) {
        String loginName = dto.getLoginName();
        String password = dto.getPassword();
        //1.校验账号是否为空 isAllBlank都不为null或空字符串时false
        if(StringUtils.isAnyBlank(loginName,password)){
            return JSONResult.error("请将信息填写完整!");
        }
        //根据login_name查询用户
        User user = loginMapper.getUserByUsername(loginName);
        //用户不存在
        if(null==user){
            return JSONResult.error("用户不存在!");
        }
        Long isDelete = user.getIsDelete();
        if(isDelete== Constant.USER_IS_DELETE){
            return JSONResult.error("账号停用了!");
        }
        String userpassword = user.getUserpassword();
        //密码不正确
        if(!StringUtils.equals(password,userpassword)){
            return JSONResult.error("密码不正确!");
        }

        //JWT颁发token
        //Header
        Map<String ,Object> header = new HashMap<>();
        //类型
        header.put(Constant.TYP,"JWT");
        //算法类型
        header.put(Constant.ALG,"hs256");

        //载荷
        Map<String,Object> payload = new HashMap<>();
        //签发时间
        payload.put(Constant.ISSUEDAT,System.currentTimeMillis());
        //时间15分钟
        payload.put(Constant.EXP,System.currentTimeMillis()+15*60*1000);
        //受众
        payload.put(Constant.AUDIENCE,user.getLoginName());
        String JWTId = SecureUtil.md5(user.getId()+user.getLoginName()+user.getUserpassword());
        //JWTId
        payload.put(Constant.JWTID,JWTId);
        //roleId
        payload.put(Constant.ROLE_ID,user.getRoleId());
        //操作人员
        payload.put(Constant.OPERATOR,user.getLoginName());
        //权限列表
        List<String> authList = roleMapper.selectAuthListByRoleId(user.getRoleId());
        payload.put(Constant.AUTHLIST,authList);
        //签证
        JWTSigner jwtSigner = JWTSignerUtil.hs256(Constant.KEY.getBytes());
        String token = JWTUtil.createToken(header, payload, jwtSigner);
        TokenVo tokenVo = new TokenVo();
        tokenVo.setToken(token);
        tokenVo.setUser(user);

        return JSONResult.success(tokenVo);
    }
}
